package com.leetcode.search;

/**
 * leetcode 529 扫雷游戏
 */
public class Solution529 {
    public char[][] updateBoard(char[][] board, int[] click) {
        if (click[0] < 0 || click[0] >= board.length
                || click[1] < 0 || click[1] >= board[0].length) {
            return board;
        }
        if (board[click[0]][click[1]] != 'M' && board[click[0]][click[1]] != 'E') {
            return board;
        }

        if (board[click[0]][click[1]] == 'M') {
            board[click[0]][click[1]] = 'X';
        } else if (board[click[0]][click[1]] == 'E') {
            updateBoard(board, click[0], click[1]);
        }
        return board;
    }

    private void updateBoard(char[][] board, int row, int column) {
        if (row < 0 || row >= board.length
                || column < 0 || column >= board[0].length) {
            return;
        }
        if (board[row][column] != 'E') {
            return;
        }

        int count = countM(board, row, column);
        if (count > 0) {
            board[row][column] = (char)('0' + count);
        } else {
            board[row][column] = 'B';
            // 左上
            updateBoard(board, row - 1, column - 1);
            // 上
            updateBoard(board, row - 1, column);
            // 右上
            updateBoard(board, row - 1, column + 1);
            // 左
            updateBoard(board, row, column - 1);
            // 右
            updateBoard(board, row, column + 1);
            // 左下
            updateBoard(board, row + 1, column - 1);
            // 下
            updateBoard(board, row + 1, column);
            // 右下
            updateBoard(board, row + 1, column + 1);
        }
    }

    private int countM(char[][] board, int row, int column) {
        int count = 0;
        if (row - 1 >= 0 && column - 1 >= 0 && board[row-1][column-1] == 'M') {
            count += 1;
        }
        if (row - 1 >= 0 && board[row-1][column] == 'M') {
            count += 1;
        }
        if (row - 1 >= 0 && column + 1 < board[row].length && board[row-1][column+1] == 'M') {
            count += 1;
        }
        if (column - 1 >= 0 && board[row][column-1] == 'M') {
            count += 1;
        }
        if (column + 1 < board[0].length && board[row][column+1] == 'M') {
            count += 1;
        }
        if (row + 1 < board.length && column - 1 >= 0 && board[row+1][column-1] == 'M') {
            count += 1;
        }
        if (row + 1 < board.length && board[row+1][column] == 'M') {
            count += 1;
        }
        if (row + 1 < board.length && column + 1 < board[0].length && board[row+1][column+1] == 'M') {
            count += 1;
        }
        return count;
    }

    public static void main(String[] args) {
        Solution529 solution529 = new Solution529();
        char[][] result = solution529.updateBoard(new char[][]{{'E', 'E', 'E', 'E', 'E'},
                {'E', 'E', 'M', 'E', 'E'},
                {'E', 'E', 'E', 'E', 'E'},
                {'E', 'E', 'E', 'E', 'E'}},
                new int[]{3, 0});
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
}
